//console.log(window.Modernizr);
   
$(document).ready(function(e) {
    
    
    var pageZoom=0,pageOffset=[],pageSize=[];
    var siteUrl='http://www.octaviachina.com';
    
    var headerBox=$('header');
    var footerBox=$('footer');
    var wrapBox=$('.wrap');
    var mainBox=$('.main');
    var pageBox=$('.pages');
    var pageBg=pageBox.children('.background');
    var pageNow=0;
    var pageLast=-1;
    var pageMax=pageBox.length;
    //console.log('pageMax:'+pageMax);
    var pageWd,pageHt;
    var pageSp=1;
    var testBox=$('#testBox');
    var testHt=testBox.outerHeight();
    var linkBar=$('#link');
    var pageScroll=0;
    var pageList=[];
    var pageTimeline=[];
    
    var loadBox=$('#popLoad');

    var submenuBox=$('.submenu').children();
    var submenuTimer;
    
    var navBox=$('nav');
    var menuBox=navBox.find('li');  
    var navBtn=navBox.children('a.btn');
    var navMask=navBox.children('.mask');
    var navTimer;
    

    
    var workPop=$('#popWorks');//内容弹出窗
    var workClose=workPop.find('.close');////内容弹出窗关闭按钮
    var workBox=workPop.children(".box");//图片和视频容器      


    var fuckAndroidReSize = false;
    


    pageWd=$(window).width();
    pageHt=$(window).height();//去掉header和footer的实际可见高度
    wrapBox.css({height: pageHt,width: pageWd});
    mainBox.css({height: pageHt,width: pageWd});
    pageBox.css({height: pageHt,width: pageWd});
    $('.justhd_ar1').css({height: pageHt,width: pageWd});
    $('.justhd_ar2').css({height: pageHt,width: pageWd});

    $( window ).on("orientationchange", function( event ) { 
        
            if(event.orientation){ 
                if(event.orientation == 'portrait'){ 
                    $(".tvcVideoBox").css({top:'150px',height:'auto'});
                    $(".tVideo").css({top:'0px'});
                    $(".vClose").css({top:'-20px'});
                    //box.hide();
                } 
                else if(event.orientation == 'landscape') { //横屏
                    $(".tvcVideoBox").css({top:'0px',height:'80%'});
                    $(".tVideo").css({top:'-80px'});
                    $(".vClose").css({top:'20px'});
                    //box.show();
                    
                } 
            } 
        });

    //第一个左右翻页
    $('.mainkv').iosSlider({
        scrollbar: true,
        snapToChildren: true,
        desktopClickDrag: true,
        navNextSelector: $('.mainkv .next'),
        navPrevSelector: $('.mainkv .prev'),
        scrollbarLocation: 'top',
        scrollbarMargin: '10px 10px 0 10px',
        scrollbarBorderRadius: '0',
        responsiveSlideWidth: true,
        navSlideSelector: $('.mainkv .iosSliderButtons .button'),
        infiniteSlider: true,
        startAtSlide: '1',
        onSlideChange: slideContentChange,
        onSlideComplete: slideContentComplete,
        onSliderLoaded: slideContentLoaded
    });

    function slideContentChange(args) {

        /* indicator */
        $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
        $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

    }

    function slideContentComplete(args) {


    }

    function slideContentLoaded(args) {

        /* indicator */
        $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
        $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

    }
    //第二个左右翻页
    $('.mainkv2').iosSlider({
        scrollbar: true,
        snapToChildren: true,
        desktopClickDrag: true,
        navNextSelector: $('.mainkv2 .next'),
        navPrevSelector: $('.mainkv2 .prev'),
        scrollbarLocation: 'top',
        scrollbarMargin: '10px 10px 0 10px',
        scrollbarBorderRadius: '0',
        responsiveSlideWidth: true,
        navSlideSelector: $('.mainkv2 .iosSliderButtons .button'),
        infiniteSlider: true,
        startAtSlide: '1',
        onSlideChange: slideContentChange,
        onSlideComplete: slideContentComplete,
        onSliderLoaded: slideContentLoaded
    });

    function slideContentChange(args) {

        /* indicator */
        $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
        $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

    }

    function slideContentComplete(args) {


    }

    function slideContentLoaded(args) {

        /* indicator */
        $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
        $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

    }




    //第三个左右翻页
    $('.mainkv3').iosSlider({
        scrollbar: true,
        snapToChildren: true,
        desktopClickDrag: true,
        navNextSelector: $('.mainkv3 .next'),
        navPrevSelector: $('.mainkv3 .prev'),
        scrollbarLocation: 'top',
        scrollbarMargin: '10px 10px 0 10px',
        scrollbarBorderRadius: '0',
        responsiveSlideWidth: true,
        navSlideSelector: $('.mainkv3 .iosSliderButtons .button'),
        infiniteSlider: true,
        startAtSlide: '1',
        onSlideChange: slideContentChange,
        onSlideComplete: slideContentComplete,
        onSliderLoaded: slideContentLoaded
    });

    function slideContentChange(args) {

        /* indicator */
        $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
        $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

    }

    function slideContentComplete(args) {


    }

    function slideContentLoaded(args) {

        /* indicator */
        $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
        $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

    }


    //第四个左右翻页
    $('.mainkv4').iosSlider({
        scrollbar: true,
        snapToChildren: true,
        desktopClickDrag: true,
        navNextSelector: $('.mainkv4 .next'),
        navPrevSelector: $('.mainkv4 .prev'),
        scrollbarLocation: 'top',
        scrollbarMargin: '10px 10px 0 10px',
        scrollbarBorderRadius: '0',
        responsiveSlideWidth: true,
        navSlideSelector: $('.mainkv4 .iosSliderButtons .button'),
        infiniteSlider: true,
        startAtSlide: '1',
        onSlideChange: slideContentChange,
        onSlideComplete: slideContentComplete,
        onSliderLoaded: slideContentLoaded
    });

    function slideContentChange(args) {

        /* indicator */
        $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
        $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

    }

    function slideContentComplete(args) {


    }

    function slideContentLoaded(args) {

        /* indicator */
        $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
        $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

    }



    //$('.page0').click(function(){
    //    $(this).removeClass('active');
    //    $(this).addClass('activet');
    //})








    //fancybox
    $('#map_img').fancybox();
    $('.img_open').fancybox();

    //打开浮层
    $('.jdd1').click(function(){
        $('.justhd_ar1').show();
        swipe_off();
        //浮层第五个左右翻页
        $('.hd_ar1').iosSlider({
            scrollbar: true,
            snapToChildren: true,
            desktopClickDrag: true,
            navNextSelector: $('.hd_ar1 .next'),
            navPrevSelector: $('.hd_ar1 .prev'),
            scrollbarLocation: 'top',
            scrollbarMargin: '10px 10px 0 10px',
            scrollbarBorderRadius: '0',
            responsiveSlideWidth: true,
            navSlideSelector: $('.hd_ar1 .iosSliderButtons .button'),
            infiniteSlider: true,
            startAtSlide: '1',
            onSlideChange: slideContentChange,
            onSlideComplete: slideContentComplete,
            onSliderLoaded: slideContentLoaded
        });

        function slideContentChange(args) {

            /* indicator */
            $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
            $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

        }

        function slideContentComplete(args) {


        }

        function slideContentLoaded(args) {

            /* indicator */
            $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
            $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

        }

    });
    $('.jdd2').click(function(){
        $('.justhd_ar2').show();
        swipe_off()
        //浮层第六个左右翻页
        $('.hd_ar2').iosSlider({
            scrollbar: true,
            snapToChildren: true,
            desktopClickDrag: true,
            navNextSelector: $('.hd_ar2 .next'),
            navPrevSelector: $('.hd_ar2 .prev'),
            scrollbarLocation: 'top',
            scrollbarMargin: '10px 10px 0 10px',
            scrollbarBorderRadius: '0',
            responsiveSlideWidth: true,
            navSlideSelector: $('.hd_ar2 .iosSliderButtons .button'),
            infiniteSlider: true,
            startAtSlide: '1',
            onSlideChange: slideContentChange,
            onSlideComplete: slideContentComplete,
            onSliderLoaded: slideContentLoaded
        });

        function slideContentChange(args) {

            /* indicator */
            $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
            $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

        }

        function slideContentComplete(args) {


        }

        function slideContentLoaded(args) {

            /* indicator */
            $(args.sliderObject).parent().find('.iosSliderButtons .button').removeClass('selected');
            $(args.sliderObject).parent().find('.iosSliderButtons .button:eq(' + (args.currentSlideNumber - 1) + ')').addClass('selected');

        }
    });
    //关闭浮层
    $('.back').click(function(){
        $('.justhd_ar1').hide();
        $('.justhd_ar2').hide();
        swipe_on();
    });






    init();
    
    function init(){
        

            androidBool=is_android();
            iphoneBool=is_iPhone();
            if(androidBool){
                //alert(navigator.userAgent)
                var arr=navigator.userAgent.split(';')
                for (var i = 0; i < arr.length; i++) {
                    var temp=arr[i].toLowerCase();
                    if(temp.indexOf('android')!=-1){
                        androidVersion=temp.split('android')[1];
                        var num=androidVersion.split('.')[0]+'.'+androidVersion.split('.')[1];
                        androidVersion=Number(num);
                        // alert(arr[i].toLowerCase()+'/'+androidVersion)
                    }
                        

                };

            }




        creatList();
        creatBg();
        
        
        //获得页面page参数
        pageNow=0;
        console.log('pageNow:'+pageNow);
        
        window_resize();
        $(window).on('resize',window_resize);





        addEvent();
        
        
    }//end func
    




// 判断是否手机段
function is_android(){
    if(navigator.userAgent.match(/Android/i)) return true;
    else return false;
}
//是否iPhone手机
function is_iPhone(){
    if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1))return true;
    else return false;
}





    function creatList(){
        var _now=-1;
        var boxes=mainBox.children();
        boxes.each(function(i, n) {
            if($(n).hasClass('pages')){
                pageList.push([_now+1,1]);
                _now++;
            }//end if
            else{
                var len=$(n).children('.pages').length;
                pageList.push([_now+1,len]);
                _now+=len;
            }//end else
        });
    }//end func





    
    //自适应
    function window_resize(){
        

        pageScroll=-pageHt*pageNow;     



        if(!fuckAndroidReSize){
            wrapBox.css({height: $(window).height(),width: $(window).width()});
            mainBox.css({height: $(window).height(),width: $(window).width()});
            pageBox.css({height: $(window).height(),width: $(window).width()});
        } 
        



        var box = $('#turnBox');



    }//end func
    

    
    
    function addEvent(){        
        
        //栏目切换
        //$(document).on('touchmove',noEvent);
        
        $(document).on('swipeup',document_swipeup);
        $(document).on('swipedown',document_swipedown);
        
        
    }//end func
    


    function creatBg(){
        pageBg.each(function(i, n) {
            var src=$(n).data('background');
            var img=$('<img>').attr({src:src}).appendTo($(n));
        });
    }//end func




    /*function window_orientationchange(e){
        var box=$('#turnBox');
        if(e.orientation=='landscape'){
            box.show();
            if(os.android) setTimeout(function(){
                box.css({width:$(window).width(),height:$(window).height()});
            },100);
        }//end if
        else box.hide();
        
        
    }//end func*/
    
    function swipe_off(e){
        $(document).off('swipeup',document_swipeup);
        $(document).off('swipedown',document_swipedown);
    }//end func 
    
    function swipe_on(e){
        $(document).on('swipeup',document_swipeup);
        $(document).on('swipedown',document_swipedown);
    }//end func
    

    

    
    

    
    function document_swipeup(e){
        //console.log('swipe up');
        if(!mainBox.hasClass('moving') && pageNow!=pageMax-1){
            pageNow++;
            pageNow=pageNow>pageMax-1?pageMax-1:pageNow;
            pageChange();
        }//end func
    }//end func
    
    function document_swipedown(e){
        //console.log('swipe down');
        if(!mainBox.hasClass('moving') && pageNow!=0){
            pageNow--;
            pageNow=pageNow<0?0:pageNow;
            pageChange();
        }//end func
    }//end func


    var s=s_gi(s_account);
    s.pageName="始终向前120年页M端";
    s.channel="始终向前120年页M端";
    s.t();
    
    function pageChange(){
        console.log(pageNow);
        if(pageNow == '0'){
        }
        else if(pageNow == '1'){
            var s=s_gi(s_account);
            s.pageName="车站概况页M端";
            s.channel="车站概况页M端";
            s.t();
        }
        else if(pageNow == '2'){
            var s=s_gi(s_account);
            s.pageName="热点车型页M端";
            s.channel="热点车型页M端";
            s.t();
        }
        else if(pageNow == '3'){
            var s=s_gi(s_account);
            s.pageName="热点车型页M端";
            s.channel="热点车型页M端";
            s.t();
        }
        else if(pageNow == '4'){
            var s=s_gi(s_account);
            s.pageName="品牌历史页M端";
            s.channel="品牌历史页M端";
            s.t();
        }
        else if(pageNow == '5'){
            $('.down_ic').show();
            var s=s_gi(s_account);
            s.pageName="精彩现场页M端";
            s.channel="精彩现场页M端";
            s.t();
        }
        else if(pageNow == '6'){
            $('.down_ic').hide();
            var s=s_gi(s_account);
            s.pageName="现场互动页M端";
            s.channel="现场互动页M端";
            s.t();
        }
        //console.log('pageNow:'+pageNow);
        //console.log('pageLast:'+pageLast);
        if(pageLast>-1) var _last=pageToMenu(pageLast);
        var _now=pageToMenu(pageNow);
        menuBox.eq(_now).addClass('active').siblings().removeClass('active');
        if(pageList[_now][1]<1 && pageLast){
            var submenu=menuBox.eq(_last).children('.submenu');
            if(submenu.length>0) submenu.hide().children().removeClass('active');
        }//end if
        else if(pageList[_now][1]>1){
            var submenu=menuBox.eq(_now).children('.submenu').children();
            var id=pageNow-pageList[_now][0];
            submenu.eq(id).addClass('active').siblings().removeClass('active');
        }//end if
        scrollFunc();
        pageLast=pageNow;
    }//end func 

    
    function pageToMenu(_now){
        var id;
        for(var i=0; i<pageList.length; i++){
            if(_now>=pageList[i][0] && _now<pageList[i][0]+pageList[i][1]){
                id=i;
                break;
            }//end if
        }//end for
        //console.log('menu_id:'+id);
        return id;
    }//end func
    
    function scrollFunc(){
        pageScroll=-pageHt*pageNow;
        mainBox.addClass('moving');
        //menuBoxOff();
        //TweenLite.to(mainBox, pageSp, {transform:'translate3d('+ 0 +'px,'+ pageScroll +'px,0px)', ease:Quart.easeInOut, onComplete:scrollComplete});
        TweenLite.to(mainBox, pageSp, {marginTop:pageScroll, ease:Quart.easeInOut, onComplete:scrollComplete});
    }//end func
    
    function scrollComplete(){
        if(pageNow>0){
            linkBar.hide();
            $('#testbar').show();
        }//end if
        else{
            linkBar.show();
            $('#testbar').hide();
        }//end else
        mainBox.removeClass('moving');
        //menuBoxOn();
    }//end func
    


    
});


//--------------------------------公共函数
    
    function noEvent(e){
        e.preventDefault();
    }//end func
    
    function randomPlus() {
            return Math.random()<0.5?-1:1;
    }//end func  随机正负
        
    function randomRange(min, max) {
            var randomNumber;
            randomNumber = Math.floor(Math.random() * (max - min + 1)) + min;
            return randomNumber;
    }//end func
    
    function getQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }//end func
    
    //打印json数据
    function jsonPrint(data){
        console.log("-----------------------------------------------------------------------------");
        for(var i=0; i<data.length; i++) objectPrint(data[i]);
        console.log("-----------------------------------------------------------------------------");
    }//end func
    
    //打印object数据
    function objectPrint(data){
        console.log("-----------------------------------------------------------------------------");
        var info="";
        for(var i in data) info+=i+":"+data[i]+"  "
        console.log(info);
        console.log("-----------------------------------------------------------------------------");
    }//end func
    
    function mathAutoSize(aryNum,aryMax){
                var aryNow=new Array()
                var aryRate= aryNum[0]/aryNum[1];
                aryNow[0] = aryMax[0];
                aryNow[1] = Math.round(aryNow[0]/aryRate);
                if(aryNow[1]<aryMax[1]){
                    aryNow[1]=aryMax[1];
                    aryNow[0] = Math.round(aryNow[1]*aryRate);
                }//end if               
                return aryNow;
    }//end func 



